package com.ys.service.base.impl;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.entity.finance.TcwPartnerBankInfo;
import com.ys.entity.sys.YsCol;
import com.ys.mapper.finance.TcwPartnerBankInfoMapper;
import com.ys.service.base.TcwPartnerBankInfoService;
import com.ys.util.CheckUtil;
import com.ys.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("tcwPartnerBankInfoService")
public class TcwPartnerBankInfoServiceImpl implements TcwPartnerBankInfoService {

    @Autowired
    private TcwPartnerBankInfoMapper tcwPartnerBankInfoMapper;

    @Autowired
    private CheckUtil checkUtil;


    @Override
    public R add(Map<String, Object> params) throws Exception {
        //验证
        String msg = yzMsg(params);
        if (msg != null){
            return R.error(msg);
        }
        TcwPartnerBankInfo tcwPartnerBankInfo = JSON.parseObject(JSON.toJSONString(params), TcwPartnerBankInfo.class);
        if (new TcwPartnerBankInfo().setVid(tcwPartnerBankInfo.getVid()).count() != 0){
            return R.error("该账户编码已存在，核实后再操作!");
        }
        if (tcwPartnerBankInfo.getBeneficiaryType() == 0){
            if (tcwPartnerBankInfo.getVlhh() == null || tcwPartnerBankInfo.getVlhh().equals("")){
                return R.error("收款人类型为第三方公司银行账号时，支行联行号不允许为空!");
            }
        }

        tcwPartnerBankInfoMapper.insert(tcwPartnerBankInfo);
        return R.ok(AddDto.gotoEdit("/web/account/tcwPartnerBankInfo/toEdit?id=" + tcwPartnerBankInfo.getId()));
    }

    /**
     * 修改公司银行信息
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R edit(Map<String, Object> params) throws Exception {

        //验证
        String msg = yzMsg(params);
        if (msg != null){
            return R.error(msg);
        }
        TcwPartnerBankInfo tcwPartnerBankInfo = JSON.parseObject(JSON.toJSONString(params), TcwPartnerBankInfo.class);
        if (new TcwPartnerBankInfo().setVid(tcwPartnerBankInfo.getVid()).where("A.id != " + tcwPartnerBankInfo.getId()).count() != 0){
            return R.error("该账户编码已存在，核实后再操作!");
        }
        if (tcwPartnerBankInfo.getBeneficiaryType() == 0){
            if (tcwPartnerBankInfo.getVlhh() == null || tcwPartnerBankInfo.getVlhh().equals("")){
                return R.error("收款人类型为第三方公司银行账号时，支行联行号不允许为空!");
            }
        }

        tcwPartnerBankInfoMapper.update(tcwPartnerBankInfo);
        return R.ok(AddDto.gotoEdit("/web/account/tcwPartnerBankInfo/toEdit?id=" + tcwPartnerBankInfo.getId()));

    }

    private String yzMsg(Map<String, Object> infoMap){
        //验证导入字段
        String backMsg = "";
        //获取pc字段数据
        List<YsCol> listField = new YsCol()
                .where("A.MenuId = "+10246L)
                .setOrderby("A.SortNo ASC")
                .queryList();
        //验证必填字段的非空；其他字段的长度限制、类型
        try {
            backMsg = checkUtil.checkTableColLengthAndDataType(
                    "TB_PartnerBankInfo",
                    listField,
                    infoMap,
                    ysColItem->ysColItem.getFormEditRequired()!=null && ysColItem.getFormEditRequired() == 1
            );
            if(backMsg!=null && !"".equals(backMsg)){//若有误，则跳过这条信息
                return backMsg;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
